@file:JvmName("MyStaticFile")

//java类中用MyStaticFile.方法调用，kotlin中直接访问
package com.example.core.utils

import android.content.res.Resources
import android.util.DisplayMetrics
import android.util.TypedValue
import android.view.View
import android.view.ViewGroup
import android.widget.Toast
import com.example.core.BaseApplication

//class Utils {
//}

//给viewgroup添加扩展属性，返回每个viewgroup的第1个子view
val ViewGroup.firstChild: View
    get() = getChildAt(0)

private val displayMetrics: DisplayMetrics = Resources.getSystem().getDisplayMetrics();
//public fun dp2px(dp: Float): Float {
//    return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, displayMetrics);
//}
//扩展函数
public fun Float.dp2px(): Float {
    return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, this, displayMetrics);
}

/*
public fun toast(string: String) {
    toast(string, Toast.LENGTH_SHORT);
}

public fun toast(string: String, duration: Int) {
    Toast.makeText(BaseApplication.currentApplication, string, duration).show();
}*/
//将以上2个方法优化为1个方法定义
@JvmOverloads//在java调用时才会显示多个重载方法
public fun toast(string: String, duration: Int = Toast.LENGTH_SHORT) {
    Toast.makeText(BaseApplication.currentApplication, string, duration).show();
}